package cn.ptu.pmaa.controller;

import cn.ptu.pmaa.entities.*;
import cn.ptu.pmaa.service.AdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    //查询所有学生账号信息
    @RequestMapping("/getAllStudentInfo")
    public RespBean getAllStudentInfo(@RequestParam int pageNum,@RequestParam int pageSize){
        HashMap<String,Object> data = new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        List<Student> allStudentInfo = adminService.getAllStudentInfo();
        PageInfo p = new PageInfo<Student>(allStudentInfo);
        data.put("pages",p.getPages());
        data.put("total",p.getTotal());
        data.put("Students",allStudentInfo);

        return new RespBean("200","查询成功",data);
    }
    //根据id查询学生账号信息
    @RequestMapping("/getStudentInfoById")
    public RespBean getStudentInfoById(@RequestParam("studentid") String id){
        HashMap<String,Object> data = new HashMap<>();
        data.put("students",adminService.getStudentInfoById(id));
        return new RespBean("200","查询成功",data);
    }
    //根据姓名查询学生账号信息
    @RequestMapping("/getStudentInfoByName")
    public RespBean getStudentInfoByName(@RequestParam("studentname") String name){
        HashMap<String,Object> data = new HashMap<>();
        data.put("students",adminService.getStudentInfoByName(name));
        return new RespBean("200","查询成功",data);
    }
    //修改学生账号信息
    @RequestMapping("/editStudentInfoById")
    public RespBean editStudentInfoById(Student student){
        System.out.println(student);
        int flag = adminService.editStudentInfoById(student);

        if (flag==1){
            return new RespBean("200","修改成功");
        }
        return new RespBean("400","修改失败");
    }
    //添加学生账号信息
    @RequestMapping("/addStudent")
    public RespBean addStudent(Student student){
        int flag=0;
        try{
            flag = adminService.addStudent(student);
        }catch (Exception e){
            e.printStackTrace();
        }

        if (flag==1){
            return new RespBean("200","添加成功");
        }
        return new RespBean("400","添加失败");
    }
    //批量添加学生账号信息
    @RequestMapping("/addAllStudent")
    public RespBean addAllStudent(@RequestParam("excelAbsoluteRoute") String excelAbsoluteRoute){
        int flag = adminService.addAllStudent(excelAbsoluteRoute);
        System.out.println(flag);
        if (flag!=0){
            return new RespBean("200","添加成功");
        }
        return new RespBean("400","添加失败");
    }
    //删除学生账号信息
    @RequestMapping("/deleteStudentById")
    public RespBean deleteStudentById(@RequestParam("studentid") String id){
        int flag = adminService.deleteStudentById(id);
        if (flag==1){
            return new RespBean("200","删除成功");
        }
        return new RespBean("400","删除失败");
    }
    //批量删除学生账号信息
    @RequestMapping("/deleteAllStudentById")
    public RespBean deleteAllStudentById(String[] ids){
        //System.out.println(Arrays.toString(ids));
        int flag = adminService.deleteAllStudentById(ids);
        if (flag!=0){
            return new RespBean("200","删除成功");
        }
        return new RespBean("400","删除失败");
    }


    //查询所有教师账号信息
    @RequestMapping("/getAllTeacherInfo")
    public RespBean getAllTeacherInfo(@RequestParam int pageNum,@RequestParam int pageSize){
        HashMap<String,Object> data = new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        List<Teacher> allTeacherInfo = adminService.getAllTeacherInfo();
        PageInfo p = new PageInfo<Teacher>(allTeacherInfo);
        data.put("pages",p.getPages());
        data.put("total",p.getTotal());
        data.put("teachers",allTeacherInfo);
        return new RespBean("200","查询成功",data);
    }
    //根据id查询教师账号信息
    @RequestMapping("/getTeacherInfoById")
    public RespBean getTeacherInfoById(@RequestParam("teacherid") String id){
        HashMap<String,Object> data = new HashMap<>();
        data.put("teachers",adminService.getTeacherInfoById(id));
        return new RespBean("200","查询成功",data);
    }
    //根据姓名查询教师账号信息
    @RequestMapping("/getTeacherInfoByName")
    public RespBean getTeacherInfoByName(@RequestParam("teachername") String name){
        HashMap<String,Object> data = new HashMap<>();
        data.put("teachers",adminService.getTeacherInfoByName(name));
        return new RespBean("200","查询成功",data);
    }
    //修改教师账号信息
    @RequestMapping("/editTeacherInfoById")
    public RespBean editTeacherInfoById(Teacher teacher){
        int flag = adminService.editTeacherInfoById(teacher);
        if (flag==1){
            return new RespBean("200","修改成功");
        }
        return new RespBean("400","修改失败");
    }
    //添加教师账号信息
    @RequestMapping("/addTeacher")
    public RespBean addTeacher(Teacher teacher){
        int flag = adminService.addTeacher(teacher);
        if (flag==1){
            return new RespBean("200","添加成功");
        }
        return new RespBean("400","添加失败");
    }
    //删除教师账号信息
    @RequestMapping("/deleteTeacherById")
    public RespBean deleteTeacherById(@RequestParam("teacherid") String id){
        int flag = adminService.deleteTeacherById(id);
        if (flag==1){
            return new RespBean("200","删除成功");
        }
        return new RespBean("400","删除失败");
    }

    //动态查询预约信息：日期，预约人角色，预约人id，开始时间，结束时间，教室，状态
    @RequestMapping("/getAppintment")
    public RespBean getAppintment(Appintment appintment,@RequestParam int pageNum,@RequestParam int pageSize){
        System.out.println(appintment);
        PageHelper.startPage(pageNum,pageSize);
        List<Appintment> appintments = adminService.getAppintment(appintment);
        HashMap<String,Object> data = new HashMap<>();
        PageInfo p = new PageInfo<Appintment>(appintments);
        data.put("pages",p.getPages());
        data.put("total",p.getTotal());
        data.put("appintments",appintments);
        return new RespBean("200","查询成功",data);
    }
    //修改预约信息
    @RequestMapping("/editAppintmentById")
    public RespBean editAppintmentById(Appintment appintment){
        int flag = adminService.editAppintmentById(appintment);
        if (flag==1){
            return new RespBean("200","修改成功");
        }
        return new RespBean("400","修改失败");
    }
    //删除预约信息
    @RequestMapping("/deleteAppintmentById")
    public RespBean deleteAppintmentById(@RequestParam("appintmentid") String id){
        int flag = adminService.deleteAppintmentById(id);
        if (flag==1){
            return new RespBean("200","删除成功");
        }
        return new RespBean("400","删除失败");
    }
    //批量删除预约信息
    @RequestMapping("/deleteAllAppintmentById")
    public RespBean deleteAllAppintmentById(@RequestParam("ids") String[] ids){
        int flag = adminService.deleteAllAppintmentById(ids);
        if (flag!=0){
            return new RespBean("200","删除成功");
        }
        return new RespBean("400","删除失败");
    }

    //管理教室信息
    //查询所有教室信息
    @RequestMapping("/getAllClassRoomInfo")
    public RespBean getAllClassRoomInfo(@RequestParam int pageNum,@RequestParam int pageSize){
        HashMap<String,Object> data = new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        List<ClassRoom> allClassRoomInfo = adminService.getAllClassRoomInfo();
        PageInfo p = new PageInfo<ClassRoom>(allClassRoomInfo);
        data.put("pages",p.getPages());
        data.put("total",p.getTotal());
        data.put("classRooms",allClassRoomInfo);
        return new RespBean("200","查询成功",data);
    }
    //修改教室信息
    @RequestMapping("/editClassRoomById")
    public RespBean editClassRoomById(ClassRoom classRoom){
        int flag = adminService.editClassRoomById(classRoom);
        if (flag==1){
            return new RespBean("200","修改成功");
        }
        return new RespBean("400","修改失败");
    }
    //添加教室信息
    @RequestMapping("/addClassRoom")
    public RespBean addClassRoom(ClassRoom classRoom){
        int flag = adminService.addClassRoom(classRoom);
        if (flag==1){
            return new RespBean("200","添加成功");
        }
        return new RespBean("400","添加失败");
    }
    //删除教室信息
    @RequestMapping("/deleteClassRoomById")
    public RespBean deleteClassRoomById(@RequestParam("classRoomId") String id){
        int flag = adminService.deleteClassRoomById(id);
        if (flag==1){
            return new RespBean("200","删除成功");
        }
        return new RespBean("400","删除失败");
    }
    //按条件查询教室信息：jsid，jsld，jsfh，jszws，sfqy，sfkfgxs
    @RequestMapping("/getClassRoomInfo")
    public RespBean getClassRoomInfo(ClassRoom classRoom,@RequestParam int pageNum,@RequestParam int pageSize){
        HashMap<String,Object> data = new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        List<ClassRoom> classRoomInfo = adminService.getClassRoomInfo(classRoom);
        PageInfo p = new PageInfo<ClassRoom>(classRoomInfo);
        data.put("pages",p.getPages());
        data.put("total",p.getTotal());
        data.put("classRooms",classRoomInfo);
        return new RespBean("200","查询成功",data);
    }

    //查看反馈信息
    @RequestMapping("/getFeedBack")
    public RespBean getFeedBack(@RequestParam int pageNum,@RequestParam int pageSize){
        HashMap<String,Object> data = new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        List<FeedBack> feedBack = adminService.getFeedBack();
        PageInfo p = new PageInfo<FeedBack>(feedBack);
        data.put("pages",p.getPages());
        data.put("total",p.getTotal());
        data.put("feedback",feedBack);
        return new RespBean("200","查询成功",data);
    }
    //删除反馈信息
    @RequestMapping("/deleteFeedBack")
    public RespBean deleteFeedBack(@RequestParam("feedbackid") String id){
        int flag = adminService.deleteFeedBack(id);
        if (flag==1){
            return new RespBean("200","删除成功");
        }
        return new RespBean("400","删除失败");
    }


}
